import {Injectable} from '@angular/core';
import {Contract} from '../types/resp/Contract';
import {ContractForm} from '../types/req/ContractForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {ContractQueryForm} from '../types/req/ContractQueryForm';

@Injectable({
  providedIn: 'root'
})
export class ContractService {
  contracts: Contract[] = [
    {"id":1,"employeeId":3,"employeeName":"王强","contractSignDate":"2023-10-22","contractType":30,"effectiveDate":"2024-01-01","expiryDate":"2026-12-31","state":20,"workLocation":"上海市浦东新区张江高科技园区","workingHours":"09:00-18:00","jobDescription":"负责环保项目的设计、实施及技术支持，确保项目符合环保标准","createTime":"2024-02-06 22:24:17","modifyTime":"2024-02-21 22:24:17"},
    {"id":2,"employeeId":2,"employeeName":"李娜","contractSignDate":"2023-06-09","contractType":20,"effectiveDate":"2023-07-01","expiryDate":"2023-12-31","state":30,"workLocation":"北京市海淀区中关村大街1号","workingHours":"09:30-17:30","jobDescription":"协助财务部完成日常账务处理及报表编制","createTime":"2024-07-24 22:24:17","modifyTime":"2024-03-12 22:24-17"},
    {"id":3,"employeeId":1,"employeeName":"张伟","contractSignDate":"2023-11-05","contractType":10,"effectiveDate":"2023-11-06","expiryDate":"2024-05-05","state":30,"workLocation":"深圳市南山区科技园","workingHours":"09:00-18:00","jobDescription":"负责软件系统的设计、开发及维护，编写高质量代码并解决技术问题","createTime":"2024-10-16 22:24:17","modifyTime":"2024-10-16 22:24:17"},
    {"id":4,"employeeId":2,"employeeName":"李娜","contractSignDate":"2023-01-21","contractType":30,"effectiveDate":"2023-02-01","expiryDate":"2026-01-31","state":20,"workLocation":"北京市海淀区中关村大街1号","workingHours":"09:30-17:30","jobDescription":"负责财务数据分析、预算编制及成本控制，提供决策支持","createTime":"2024-05-14 22:24:17","modifyTime":"2024-08-19 22:24:17"},
    {"id":5,"employeeId":4,"employeeName":"陈静","contractSignDate":"2023-03-23","contractType":30,"effectiveDate":"2023-04-01","expiryDate":"2026-03-31","state":20,"workLocation":"成都市高新区天府大道","workingHours":"09:00-18:00","jobDescription":"负责市场调研、品牌推广及销售策略制定，带领团队完成销售目标","createTime":"2024-06-10 22:24:17","modifyTime":"2024-03-22 22:24:17"},
    {"id":6,"employeeId":3,"employeeName":"王强","contractSignDate":"2023-03-18","contractType":30,"effectiveDate":"2023-04-01","expiryDate":"2026-03-31","state":20,"workLocation":"上海市浦东新区张江高科技园区","workingHours":"09:00-18:00","jobDescription":"负责环保项目的整体规划、执行及交付，协调团队资源并确保项目按时完成","createTime":"2024-09-02 22:24:17","modifyTime":"2024-10-13 22:24:17"},
    {"id":7,"employeeId":1,"employeeName":"张伟","contractSignDate":"2023-11-06","contractType":20,"effectiveDate":"2023-11-07","expiryDate":"2024-05-06","state":30,"workLocation":"深圳市南山区科技园","workingHours":"09:00-18:00","jobDescription":"协助开发团队完成软件系统的设计与开发，学习并应用最新技术","createTime":"2024-04-28 22:24:17","modifyTime":"2024-11-06 22:24:17"}
  ]
  constructor() {
  }

  save (contractForm: ContractForm): Observable<Result<void>> {
    console.log('contractApi save params', contractForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (contractQueryForm: ContractQueryForm): Observable<Result<Page<Contract>>> {
    console.log('contractApi find params', contractQueryForm)
    return MockApi.queryPageSuccessfully(this.contracts).pipe(
      map((response: Response<Result<Page<Contract>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Contract>> {
    console.log('contractApi findById id', id)
    let target = {}
    for (const a of this.contracts) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Contract>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('contractApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
